/*
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package jsr203.nio.file;

import java.io.Closeable;
import java.util.Iterator;

/**
 * An object to iterate over the entries in a directory. A directory stream
 * allows for convenient use of the for-each construct:
 * <pre>
 *   DirectoryStream stream = dir.newDirectoryStream(filter);
 *   try {
 *       for (DirectoryEntry entry: stream) {
 *         ..
 *       }
 *   } finally {
 *       stream.close();
 *   }
 * </pre>
 *
 * <p> A {@code DirectoryStream} is opened upon creation and has an associated
 * {@link Iterator} that is obtained by invoking the {@link #iterator iterator}
 * method. The iterator may only be obtained once; a second, or subsequent, call
 * to the {@link #iterator() iterator} method throws an {@code IllegalStateException}.
 * A {@code DirectoryStream} is closed by invoking the {@link #close close}
 * method. Closing the directory stream releases any resources associated with
 * the stream. The {@link Files#withDirectory Files.withDirectory} utility method
 * is useful for cases where a task is performed on entries in a directory. This
 * method automatically closes the directory stream when iteration is complete
 * (or an error occurs). Once a directory stream is closed, all further method
 * invocations on the iterator throw {@link
 * java.util.concurrent.ConcurrentModificationException} with cause
 * {@link ClosedDirectoryStreamException}.
 *
 * <p> A directory stream is not required to be <i>asynchronously closeable</i>.
 * If a thread is blocked on the directory stream's iterator, reading from the
 * directory, and another thread invokes the {@code close} method then it may
 * require to block until the read operation is complete.
 *
 * <p> The {@link Iterator#hasNext() hasNext} and {@link Iterator#next() next}
 * methods can encounter an I/O error when iterating over the directory in which
 * case {@code ConcurrentModificationException} is thrown with cause
 * {@link java.io.IOException}. The {@code hasNext} method is guaranteed to
 * read-ahead by at least one element. This means that if the {@code hasNext}
 * method returns {@code true} and is followed by a call to the {@code next}
 * method then it is guaranteed not to fail with a {@code
 * ConcurrentModificationException}.
 *
 * <p> The elements returned by the iterator are of type {@link DirectoryEntry},
 * each one representing an entry in the directory. The elements are returned in
 * no specific order. Some file systems maintain links to the directory itself and
 * the directory's parent directory. Entries representing these links are not
 * returned by the iterator.
 *
 * <p> The iterator's {@link Iterator#remove() remove} method removes the
 * directory entry for the last element returned by the iterator, as if by
 * invoking the {@link FileRef#delete delete} method with the {@code
 * failIfNotExists} parameter set to {@code true}. If an I/O error occurs
 * then {@code ConcurrentModificationException} is thrown with cause
 * {@code IOException}. Where the directory is associated with a {@link
 * FileSystem} that does not provide {@link FileSystem#isReadOnly read-write}
 * access then {@link UnsupportedOperationException} is thrown with cause {@link
 * ReadOnlyFileSystemException}. Note that this different from the case where
 * the underlying {@link FileStore} is {@link FileStore#isReadOnly read-only}.
 * In that case, {@code ConcurrentModificationException} is thrown with an
 * appropriate {@code IOException} as cause.
 *
 * <p> The iterator is <i>weakly consistent</i>. It is thread safe but does not
 * freeze the directory while iterating, so it may (or may not) reflect updates
 * to the directory that occur after the {@code DirectoryStream} is created.
 *
 * @since 1.7
 *
 * @see FileRef#newDirectoryStream
 */

public interface DirectoryStream
    extends Closeable, Iterable<DirectoryEntry>
{
    /**
     * A task that decides if a directory entry should be accepted or filtered.
     * A {@code Filter} is passed as the parameter to {@link
     * FileRef#newDirectoryStream newDirectoryStream} method when opening a
     * directory to iterate over the entries in the directory.
     *
     * <p> The {@link DirectoryStreamFilters} class defines factory methods to
     * create filters for a number of common usages and also methods to combine
     * filters.
     *
     * @since 1.7
     */
    public static interface Filter {
        /**
         * Decides if the given directory entry should be accept or filtered.
         *
         * @param   entry
         *          The directory entry to be tested
         *
         * @return  {@code true} if the directory entry should be accepted
         */
        boolean accept(DirectoryEntry entry);
    }

    /**
     * Returns the iterator associated with this {@code DirectoryStream}.
     *
     * @return  The iterator associated with this {@code DirectoryStream}
     *
     * @throws  IllegalStateException
     *          If this directory stream is closed or the iterator has already
     *          been returned
     */
    @Override
    Iterator<DirectoryEntry> iterator();
}
